package com.example.transit.contentprovider;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.transit.bean.CollectionBean;
import com.example.transit.bean.HistoryBean;

import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

public class DbCollectionInfo {
    private static final String TAG = "DbCollectionInfo";
    private ContentResolver mContentResolver;
    private DbHelper dbHelper;

    public DbCollectionInfo(ContentResolver mContentResolver, Context context) {
        this.mContentResolver = mContentResolver;
        this.dbHelper=new DbHelper(context);
    }
   public void insertCollectInfo(CollectionBean info){
       ContentValues cv=new ContentValues();
       String[] projection={DbHelper.COLUME_HISTORY_ID};
       int id=0;
       Cursor cursor = mContentResolver.query(DbContentProvider.CONTENT_URI_HISTORYINFO, projection, null, null, "ID DESC");
       if (cursor.moveToFirst()){
           id=cursor.getInt(0);
       }
       id++;
       cv.put(DbHelper.COLUME_HISTORY_ID, id);
       cv.put(DbHelper.COLUME_HISTORY_LINE, info.getLineNum());
       cv.put(DbHelper.COLUME_HISTORY_NAME, info.getName());
       cv.put(DbHelper.COLUME_HISTORY_DIRECT, info.getIsupline());
       mContentResolver.insert(DbContentProvider.CONTENT_URI_HISTORYINFO,cv);
       cursor.close();
   }
   //查重并删除
    public Boolean Dup_check(CollectionBean info){
        SQLiteDatabase db =dbHelper.getWritableDatabase();
        int isupline=0;
        if (info.getIsupline()){
            isupline=1;
        }else {
            isupline=0;
        }
        String sql= "select * from "+DbHelper.TABLE_HISTORY+" where "+DbHelper.COLUME_HISTORY_LINE+" = '"+info.getLineNum()+"' and "+DbHelper.COLUME_HISTORY_NAME+" = '"+info.getName()+"' and "+DbHelper.COLUME_HISTORY_DIRECT+" = "+isupline;
        Cursor cursor= db.rawQuery(sql, null);
        Log.e("busappmas", "sql"+sql);
        if(cursor.getCount()!=0){
            cursor.close();
            return true;

        }else {
            return false;
        }
    }
    //删除点击的条目
    public void deleteCollect(CollectionBean info){
        SQLiteDatabase db =dbHelper.getWritableDatabase();
        int isupline=0;
        if (info.getIsupline()){
            isupline=1;
        }else {
            isupline=0;
        }
        String sql= "delete from "+DbHelper.TABLE_HISTORY+" where "+DbHelper.COLUME_HISTORY_LINE+" = '"+info.getLineNum()+"' and "+DbHelper.COLUME_HISTORY_NAME+" = '"+info.getName()+"' and "+DbHelper.COLUME_HISTORY_DIRECT+" = "+isupline;
        db.execSQL(sql);
    }

    public List<CollectionBean> getAllCollection(){
        List<CollectionBean> list=new ArrayList<>();
        list.clear();
        String[] projection ={DbHelper.COLUME_HISTORY_LINE,DbHelper.COLUME_HISTORY_NAME,DbHelper.COLUME_HISTORY_DIRECT};
        Cursor cursor = mContentResolver.query(DbContentProvider.CONTENT_URI_HISTORYINFO, projection, null,null, null);

        if ( cursor!=null && cursor.getCount() != 0 ){

            while ( cursor.moveToNext() ) {
                CollectionBean collectionBean = new CollectionBean();
                collectionBean.setLineNum(cursor.getString(0));
                collectionBean.setName(cursor.getString(1));
                Boolean value = cursor.getInt(2)>0?true:false;
                Log.d("may", "cursor.getInt(2): " + cursor.getInt(2)+"boolean value:"+value);
                collectionBean.setIsupline(value);
                list.add(collectionBean);
            }
        }
        cursor.close();
        return list;
    }


}
